package com.wy.scs.rabbitmq.producer.controller;

import com.wy.scs.rabbitmq.producer.message.MessageData;
import com.wy.scs.rabbitmq.producer.message.MySource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author wangyong
 */
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private MySource mySource;

    @GetMapping("/send_broadcast")
    public boolean send() {
        // 创建 Message
        MessageData message = new MessageData();
        message.setId(new Random().nextInt());
        // 创建 Spring Message 对象
        Message<MessageData> springMessage = MessageBuilder.withPayload(message)
                .build();
        log.info("生产者发送消息：消息编号={}", message.getId());
        // 发送消息
        return mySource.demoOutput().send(springMessage);
    }

}
